<template>
  <div class="member-order">
    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name"></XtxTabsPanel>
    </XtxTabs>

    <!-- 订单列表 -->
    <div class="order-list">
      <!-- 封装单个列表组件 -->
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        v-for="item in orderList"
        :key="item.id"
        :order="item"
        @on-cancel-order="handlerCancel"
        @on-delete-order="handlerDelete"
        @on-confirm-order="handlerConfirm"
        @on-logistics="handlerLogistics"
      />
    </div>

    <!-- 分页组件 -->
    <XtxPagination v-if="total > requestParams.pageSize" @current-change="requestParams.page = $event" :total="total" :page-size="requestParams.pageSize" :current-page="requestParams.page" />

    <!-- 取消原因组件 -->
    <OrderCancel ref="orderCancelCom" />
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsCom" />
  </div>
</template>
<script>
import { ref, reactive, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import { findOrderList, deleteOrder, confirmOrder } from '@/api/order'
import OrderItem from './components/order-item'
import OrderCancel from './components/order-cancel'
import OrderLogistics from './components/order-logistics'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'

export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup() {
    // 1、选中的选项卡
    const activeName = ref('all')
    // 2、点击选项卡tabs + 4、切换
    const tabClick = (tab) => {
      // 此时：tab.index 就是订单的状态
      requestParams.orderState = tab.index
      requestParams.page = 1
    }

    // 3.1查询订单参数
    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 3.2订单列表
    const orderList = ref([])
    // 4.1 loading处理
    const loading = ref(false)
    // 4.1 分页
    const total = ref(0)

    // 3、查询订单
    const getOrderList = () => {
      loading.value = true
      findOrderList(requestParams).then((data) => {
        orderList.value = data.result.items
        loading.value = false
        total.value = data.result.counts
      })
    }
    // 4.1 点击切换后，通过watch 监听触发tabs切换后数据改变
    watch(
      requestParams,
      () => {
        getOrderList()
      },
      { immediate: true }
    )
    // 5、删除订单
    const handlerDelete = (order) => {
      Confirm({ text: '亲，您确认删除该订单吗？' })
        .then(() => {
          deleteOrder(order.id).then(() => {
            Message({ type: 'success', text: '删除成功！' })
            getOrderList()
          })
        })
        .catch((e) => {
          Message({ text: `${e}` })
        })
    }
    return {
      activeName,
      tabClick,
      orderStatus,
      orderList,
      loading,
      requestParams,
      total,
      handlerDelete,
      ...useCancelOrder(),
      ...useConfirmOrder(),
      ...useLogisticsOrder()
    }
  }
}

// 4、取消订单逻辑（封装逻辑，其他地方可直接调用）
export const useCancelOrder = () => {
  // 组件实例 ref
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerCancel = (order) => {
    // item 就是你要取消的订单
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}

// 6、确认收货（封装逻辑，其他地方可直接调用）
export const useConfirmOrder = () => {
  const handlerConfirm = (order) => {
    // order 就是你要确认收货的订单
    Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' })
      .then(() => {
        confirmOrder(order.id).then(() => {
          Message({ text: '确认收货成功', type: 'success' })
          // 确认收货后状态变成 待评价
          order.orderState = 4
        })
      })
      .catch((e) => {
        Message({ text: `${e}` })
      })
  }
  return { handlerConfirm }
}

// 7、查看物流（封装逻辑，其他地方可直接调用）
export const useLogisticsOrder = () => {
  const orderLogisticsCom = ref(null)
  const handlerLogistics = (item) => {
    orderLogisticsCom.value.open(item)
  }
  return { handlerLogistics, orderLogisticsCom }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
